/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package giormi;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Ling
 */
public class F_addorderstocks {
    
    private int os_idorder;
    private int os_idstock;

    public void setOs_idorder(int os_idorder) {
        this.os_idorder = os_idorder;
    }

    public void setOs_idstock(int os_idstock) {
        this.os_idstock = os_idstock;
    }

    private Connection conn;
    private String SQLStatement;
    private String URL;
    private Statement stmt;
    public ResultSet rset;

    public void connect(){
        try{
            DBConnection db = new DBConnection();
            conn = DBConnection.get();
            stmt = conn.createStatement();
        }
        catch(Exception e){
            System.out.println(e.getMessage());
            JOptionPane.showMessageDialog(null,e.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }

    public void disconnect(){
        try{
            stmt.close();
            conn.close();
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
            JOptionPane.showMessageDialog(null,ex.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }

    public int getidorder_stock(){
        int idorder_stock = 0;
        connect();
        try {
            SQLStatement = "SELECT idorder_stock FROM order_stock "+
                    "WHERE os_idorder ="+os_idorder;
            rset = stmt.executeQuery(SQLStatement);
            if (rset.next()) {
              idorder_stock = rset.getInt(1);
              System.out.println(rset.getInt(1));
            }
            rset.close();
            disconnect();
        } catch (SQLException ex) {
            Logger.getLogger(F_addstock.class.getName()).log(Level.SEVERE, null, ex);
        }
        return idorder_stock;
    }

    public void addorder_stock(){
        connect();
        try{
           SQLStatement = "INSERT INTO order_stock "+
                   "(os_idorder, os_idstock) VALUES"+
                   "('"+os_idorder+"','"+os_idstock+"')";
            stmt.executeUpdate(SQLStatement);
            disconnect();
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
            JOptionPane.showMessageDialog(null,ex.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }

}
